<template>
    <div class='academic-paper'>
        <Search btns="2-1" v-if="isFlash" :searchData="formaltSearchData" :search="search"></Search>
        <Table btns="2-3" :tableData="tableData0||tableData" :tableTotable="true" :data0="data" :params="pageInfo"
               :search="search"></Table>
        <p style="display:none">{{getData}}</p>
        <Add :model="addmodel" :form="form" @reflash="getList"/>
        <Fields :model="addmodel" :form="form" @reflash="flashSearch"/>

    </div>
</template>
<script>
    import Search from "../../common/search"
    import Table from "../../common/table"
    import Add from "./common/add"
    import Fields from "./common/fields"

    export default {
        props: ["searchData0", "tableData0", "tablename0"],
        components: {Search, Table, Add,Fields},
        name: "",
        data() {
            return {
              isFlash:true,
                search: {},
                form: {},
                pageInfo: {
                    page: 1,
                    total: 0,
                    tablename: this.tablename0 || this.$store.state.tablename || "outcomes_academic"
                },
                searchData: [
                    {key: "", name: "论文类型", type: "select", array: []},
                    {key: "", name: "第一作者", type: "select", array: []},
                    {key: "", name: "第二作者", type: "select", array: []},
                    {key: "", name: "其他作者", type: "select", array: []},
                    {key: "", name: "通讯作者", type: "select", array: []},
                    {key: "", name: "期刊名称", type: "select", array: []},
                    {key: "conference", name: "会议名称", type: "select", array: []},
                    {key: "", name: "中文名称", type: "select", array: []},
                    {key: "", name: "主办单位", type: "select", array: []},
                    {key: "", name: "会议届次", type: "select", array: []},
                    {key: "", name: "举办地点", type: "select", array: []},
                    {key: "", name: "论文标题", type: "input"},
                    {key: "", name: "标准刊号", type: "select", array: []},
                    {key: "", name: "刊发年度", type: "select", array: []},
                    {key: "", name: "刊发月份", type: "select", array: []},
                    {key: "", name: "期号", type: "input"},
                    {key: "", name: "卷号", type: "input"},
                    {key: "", name: "页码", type: "input"},
                    {key: "", name: "EI检索", type: "input"},
                    {key: "", name: "SCI检索", type: "input"},
                    {key: "cnki", name: "中文文献地址", type: "input"},
                ],
                tableData: [
                    {
                        label: "基本信息",
                        data: [
                            {props: "add", label: "填报人", width: ""},
                            {props: "", label: "论文类型", width: ""},
                            {props: "", label: "论文标题", width: "",},
                            {props: "", label: "第一作者", width: ""},
                            {props: "", label: "第二作者", width: ""},
                            {props: "", label: "其他作者", width: ""},
                            {props: "", label: "通讯作者", width: ""},
                            {props: "", label: "期刊名称", width: ""},
                        ]
                    }, {
                        label: "会议信息",
                        data: [
                            {props: "", label: "会议名称（缩写）", width: ""},
                            {props: "", label: "中文名称", width: ""},
                            {props: "", label: "会议届次", width: ""},
                            {props: "", label: "举办地点", width: ""},
                            {props: "", label: "主办单位", width: ""},
                            {props: "", label: "参会时间", width: ""},
                        ]
                    }, {
                        label: "",
                        data: [
                            {props: "", label: "标准代号", width: ""},
                            {props: "", label: "刊发年度", width: ""},
                            {props: "", label: "刊发月份", width: ""},
                            {props: "", label: "期号", width: ""},
                            {props: "", label: "卷号", width: ""},
                            {props: "", label: "页码范围", width: ""},
                        ]
                    }, {
                        label: "检索文献标识",
                        data: [
                            {props: "", label: "EI检索", width: ""},
                            {props: "", label: "SCI检索", width: ""},
                        ]
                    }, {
                        label: "",
                        data: [
                            {props: "", label: "中文文献（CNKI）地址", width: ""},
                            {props: "", label: "备注", width: ""},
                        ]
                    }
                ],
                addmodel: [],
                // 列表数据
                data: [{}],
                settime: null
            };
        },
        computed: {
            getData() {
                if (this.settime) clearTimeout(this.settime)
                var params = {...this.search, ...{page: this.pageInfo.page}}
                this.settime = setTimeout(() => {
                    this.getList(params)
                    return true
                }, 500)
            },

            formaltSearchData(){
              if(Array.isArray(this.searchData0))this.searchData=this.searchData0
              return []||this.searchData
            }
        },
        methods: {
            flashSearch(){
              this.isFlash=false
              this.getSelectData()
              setTimeout(()=>{this.isFlash=true},100)
            },
            /**
             * @name 通过标题获取论文详情
             */
            getPaperInfo(item) {
                if (!this.form[item.prop]) return
                var params = {
                    key: item.prop,
                    value: this.form[item.prop],
                    tablename: this.tablename0 || this.$store.state.tablename || "outcomes_academic"
                }
                _g.apiPost("teacher/liketitle", params).then(res => {
                    for (var key in res.data) {
                        if (key.indexOf("author") > -1 && !Array.isArray(res.data[key]) && res.data[key].length > 0) {
                            res.data[key] = res.data[key].split(",")
                        } else if (key.indexOf("author") > -1) {
                            res.data[key] = []
                        }
                    }
                    this.$set(this, "form", res.data ? res.data : this.form)
                })
            },

            /**
             * @name 获取列表
             */
            getList(params) {
                var goal = params || {...this.search, ...{page: this.pageInfo.page}}
                goal.tablename = this.tablename0 || this.$store.state.tablename || this.pageInfo.tablename
                delete goal.sepcialSelect_0
                delete goal.sepcialSelect_1
                delete goal.sepcialSelect_2
                delete goal.sepcialSelect_3
                delete goal.sepcialSelect_4

                _g.apiPost("teacher/listsall", goal).then(res => {
                    // if (res.data.list.length > 0) this.$set(this, "data", res.data.list)
                    this.$set(this, "data", res.data.list)
                    this.pageInfo.total = res.data.total
                })
            },

            /**
             * @name 获取下拉
             */
            getSelectData() {
                var params = {
                    fields: [],
                    name: this.tablename0 || this.$store.state.tablename || "outcomes_academic"
                }
                let data0 = this.searchData0 || this.searchData
                
                data0.forEach(ele => {
                    if (ele.type == "select") {
                        if (ele.key) params.fields.push(ele.key)
                    }
                })
                _g.apiPost('teacher/getselect', params).then(res => {
                    data0.forEach(ele => {
                        if (ele.key && ele.type == "select") {
                            this.$set(ele, "array", res.data[ele.key])
                        }
                    })
                })
            },

            getheadname() {
                var path = _g.getRouterPath(this)
                let blurArray = [
                    {key: '/academicpaper', values: ['title']}
                ];
                let searchArray = [
                    {key: "/academicpaper", values: ['author', 'author_2', 'author_other', 'author_comm']},
                    {key: "/teachmaterial", values: ['editor', 'subeditor', 'editor_other']},
                    {key: "/softwork", values: ['author', 'author_2', 'author_other']},
                    {key: "/teachingpaper", values: ['author']},
                    {key: "/researchs", values: ['principal']},
                    {key: "/classChange", values: ['teacher']},
                    {key: "/famoust", values: ['name']},
                    {key: "/guidance", values: ['teacher']},
                    {key: "/otherteaching", values: ['chief']},
                ];//可以搜索的

                _g.apiPost("teacher/tableall", {tablename: (this.$store.state.tablename || this.tablename0 || this.pageInfo.tablename)}).then(res => {
                    var map = new Map(res.data)
                    let addmodel = []
                    let data0 = this.searchData0 || this.searchData
                    let data1 = this.tableData0 || this.tableData

                    // add 20190121
                    this.$store.state.dia.export.info=res.data


                    map.forEach((val, key) => {
                        data0.forEach(ele => {
                            if (ele.name === val) ele.key = key
                        })

                        data1.forEach(ele => {
                            ele.data.forEach(ele1 => {
                                if (ele1.label === val) ele1.props = key
                            })
                        })

                        addmodel.push({
                            prop: key,
                            name: val,
                            blur: blurArray.findIndex(val => val.key === path && val.values.includes(key)) > -1 ? this.getPaperInfo : "",
                            canSearch: searchArray.findIndex(val => val.key === path && val.values.includes(key)) > -1 ? true : false,
                        })

                        this.$set(this, "addmodel", addmodel)

                        // this.getSelectData()
                    })
                }).then(() => {
                    // console.log(this.searchData0||this.searchData)
                    this.getSelectData()
                })
            }
        },
        created() {
            this.getheadname()
            if (bus._events.flash !== null) {
                bus.$off("flash")
            }
            bus.$on("flash", (params) => {
                this.getList()
            })
        },
    };
</script>
<style scoped>
    .academic-paper {
    }
</style>